﻿namespace MsForgeReboot.Web.Areas.ControlPanel.Controllers {
    #region Usings

    using System;
    using System.Linq;
    using System.Web.Mvc;
    using PagedList;
    using MsForgeReboot.Framework.Entities;
    using MsForgeReboot.Framework.Interfaces;

    using WebMatrix.WebData;

    #endregion

    public class BlogController : Controller {
        #region Constants and Fields

        private readonly IBlogService blogService;

        private readonly IUserService userService;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="BlogController" /> class.
        /// </summary>
        /// <param name="blogService">The blog service instance.</param>
        /// <param name="userService">The user service.</param>
        public BlogController(IBlogService blogService, IUserService userService) {
            this.blogService = blogService;
            this.userService = userService;
        }

        #endregion

        #region Public Methods and Operators

        [HttpPost]
        public ActionResult Create(BlogPost model) {

            model.Author = this.userService.GetSingleUser(User.Identity.Name);

            if (this.ModelState.IsValid) {
                this.blogService.Save(model);
                return this.RedirectToAction("Index");
            }

            ViewBag.Title = "Create Blog Post";
            return this.View("Edit", model);
        }

        public ActionResult Create() {
            ViewBag.Title = "Create Blog Post";
            return this.View(
                "Edit",
                new BlogPost {
                    Title = "Enter Title",
                    Published = DateTime.Now,
                    Author = this.userService.GetSingleUser(User.Identity.Name)
                });
        }

        [HttpPost]
        public ActionResult Edit(BlogPost model) {

            model.Author = this.userService.GetSingleUser(User.Identity.Name);
            
            if (this.ModelState.IsValid) {
                this.blogService.Save(model);
                return this.RedirectToAction("Index");
            }
            
            ViewBag.Title = "Edit Blog Post";
            return this.View(model);
        }

        public ActionResult Edit(string urlPointer) {
            ViewBag.Title = "Edit Blog Post";
            var model = this.blogService.GetByUrlPointer(urlPointer);
            model.Author = this.userService.GetSingleUser(User.Identity.Name);

            return this.View(model);
        }

        public ActionResult Index(int page = 1, int pageSize = 10) {
            ViewBag.Title = "Blog Posts";
            return this.View(this.blogService.GetList(page,pageSize));
        }
        
        [HttpPost]
        public ActionResult Delete(string urlPointer)
        {
            this.blogService.Delete(urlPointer);
            return Json("success", JsonRequestBehavior.DenyGet);
        }

        public ActionResult Tags(string urlPointer, int pageNumber = 1, int pageSize = 10) {
            var tag = this.blogService.GetTagWithBlogPosts(urlPointer, pageNumber, pageSize);
            ViewBag.Title = tag.Name;

            return this.View("Index", (PagedList<BlogPost>)tag.BlogPosts);
        }

        #endregion
        
    }
}